﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;

namespace WebC
{
    public partial class system_useredit : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(system_useredit));
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        public void loadInfo()
        {
            clsSys clss = null;
            tblLoginInof.Visible = false;
            string sModelName = "";
            try
            {
                clss = new clsSys();
                if (m_Id == 0)
                {
                    tblLoginInof.Visible = false;
                    if (!checkModelRightById(12, ref sModelName, (clsBaseLib)clss))
                    {
                        errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                        return;
                    }
                }
                else
                {
                    if (Convert.ToInt32(Request["noedit"]) == 1)
                    {
                        if (!checkModelRightById(11, ref sModelName, (clsBaseLib)clss))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                        tblLoginInof.Visible = true;
                    }
                    else
                    {
                        if (!checkModelRightById(14, ref sModelName, (clsBaseLib)clss))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    DataTable dt = clss.getUserInfoById(m_Id);
                    txtUserName.Text = Convert.ToString(dt.Rows[0]["username"]);
                    txtLoginCode.Text = Convert.ToString(dt.Rows[0]["logincode"]);
                    txtUserDesc.Text = Convert.ToString(dt.Rows[0]["userdesc"]);
                    hfDept.Value = Convert.ToString(dt.Rows[0]["deptid"]);
                    inpDept.Value = Convert.ToString(dt.Rows[0]["deptname"]);
                    txtAddress.Text = Convert.ToString(dt.Rows[0]["address"]);
                    txtPhone.Text = Convert.ToString(dt.Rows[0]["phone"]);
                    lbLastLogin.Text = dt.Rows[0]["lastlogin"] == DBNull.Value ? "" : Convert.ToDateTime(dt.Rows[0]["lastlogin"]).ToString("yyyy-MM-dd HH:mm");
                    lbLoginTime.Text = Convert.ToString(dt.Rows[0]["logintimes"]);
                }
                sModelFullName = sModelName;

            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (clss != null)
                {
                    clss.Dispose();
                }
            }
        }

        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("userlist.aspx");
        }

        [Ajax.AjaxMethod]
        public bool checkLoginCode(string sLoginCode)
        {
            clsSys clss = null;
            bool bResult = false;
            try
            {
                clss = new clsSys();
                bResult = clss.checkLoginCode(sLoginCode.Replace("'", "''"));
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if(clss!=null)
                {
                    clss.Dispose();
                }
            }
            return bResult;
        }

        [Ajax.AjaxMethod]
        public bool saveData(int iUid, string sName, string sCode, int iDeptId, string sDesc,string sAddress,string sPhone)
        {
            clsSys clss = null;
            bool bResult = false;            
            sName = sName.Replace("'", "''");
            sCode = sCode.Replace("'", "''");
            sDesc = sDesc.Replace("'", "''");
            sAddress = sAddress.Replace("'", "''");
            sPhone = sPhone.Replace("'", "''");

            try
            {
                clss = new clsSys();

                bResult = clss.setUserInfo(iUid, sName, sCode, iDeptId, sDesc, sAddress, sPhone);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (clss != null)
                {
                    clss.Dispose();
                }
            }
            return bResult;
        }
    }
}